using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SlothEngine.Core;
using SlothEngine.Input;
using SlothEngine.Physics;
using SlothEngine.Physics.ParticleEffects;

namespace EngineTest
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        public SpriteFont font;

        // Fountain test variables
        Texture2D partImage;
        Vector2 fountainPosition;
        FountainEffect fountain;

        // Explosion test variables
        Texture2D smokeImage;
        Texture2D explosionImage;
        Vector2 explosionPosition;
        ExplosionEffect explosion;
        bool explode;

        // Input test variables
        InputManager input;
        Vector2 mousePosition;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            input = new InputManager();
        }

        protected override void Initialize()
        {
            Log.Setup();

            fountainPosition.X = graphics.PreferredBackBufferWidth / 2;
            fountainPosition.Y = graphics.PreferredBackBufferHeight / 3;

            explosionPosition.X = graphics.PreferredBackBufferWidth / 3;
            explosionPosition.Y = 2 * graphics.PreferredBackBufferHeight / 3;
            explode = false;

            input.AddKeyboard(new XNAKeyboard(PlayerID.One));
            input.AddMouse(new XNAMouse(PlayerID.One));
            mousePosition = Vector2.Zero;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Content.Load<SpriteFont>("font");

            partImage = Content.Load<Texture2D>("testParticle");
            fountain = new FountainEffect(fountainPosition, new TimeSpan(0, 0, 3), 512, partImage, new Vector2(-20, -80), new Vector2(20, -50));

            smokeImage = Content.Load<Texture2D>("smokeParticle");
            explosionImage = Content.Load<Texture2D>("explosionParticle");
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (input.KeyPressed(PlayerID.All, Keys.Escape))
            {
                Log.Close();
                this.Exit();
            }

            // Test input and logging.
            if (input.KeyPressed(PlayerID.All, Keys.F1))
                Log.PrintLine("F1 key was pressed");

            if (input.KeyHeld(PlayerID.All, Keys.F2))
                Log.PrintLine("F2 key held.", Color.Red);

            if (input.KeyReleased(PlayerID.All, Keys.F2))
                Log.PrintLine("F2 key was Released.", Color.Magenta);

            mousePosition = input.MousePosition(PlayerID.All);


            // Test fountain
            fountain.Update(gameTime);

            // Test explosion by creating an explosion when space is pressed
            if (input.KeyPressed(PlayerID.All, Keys.Space))
            {
                CreateExplosion();

                explode = true;
            }
            if (explode)
                explosion.Update(gameTime);


            input.Update(gameTime);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.DrawString(font, "Position: " + mousePosition.X.ToString() + ", " + mousePosition.Y.ToString(), new Vector2(20, 20), Color.White);
            
            fountain.Draw(spriteBatch);
            
            if (explode)
                explosion.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void CreateExplosion()
        {
            explosion = new ExplosionEffect(explosionPosition, smokeImage, new TimeSpan(0, 0, 0, 2, 300), explosionImage, new TimeSpan(0, 0, 0, 1, 500));
            explosion.BackgroundMinVelocity = 15;
            explosion.BackgroundMaxVelocity = 30;
            explosion.ForegroundMinVelocity = 15;
            explosion.ForegroundMaxVelocity = 30;
        }
    }
}
